package com.ms.game.itembox;

import java.lang.reflect.Type;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ms.game.itembox.container.ItemJsonContent;
import com.ms.game.itembox.db.ItemBoxModel;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.game.system.InitData;
import com.xpec.c4.game.utility.GUIDFactory;

public class ClientItemJsonHelper {

	private static final Log log = LogFactory.getLog(ClientItemJsonHelper.class);
	
	private static boolean useDefault(ItemBoxModel itemBoxModel){

		if ((itemBoxModel.getDefaultItemId().equals("BONUS_PACK") || 
				itemBoxModel.getDefaultItemId().equals("TREASURE_BOX") || 
				itemBoxModel.getDefaultItemId().equals("GIFT_BOX")) || 
				itemBoxModel.getDefaultItemId().equals("FNBO_VOUCHER")){
			return false;
		}
		return true;
	}
	
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		System.setProperty("log4j.configurationFile", "./config/log4j2.xml");
		InitData.initAll();
		GUIDFactory.setWorldId(1);
		GUIDFactory.setProcessId(1);

		Session session = HibernateManager.getSession();
		ArrayList<ItemBoxModel> models = (ArrayList<ItemBoxModel>) session.createQuery(
				"From ItemBoxModel Where " + 
				"((defaultItemId = 'FNBO_VOUCHER' or defaultItemId = 'BONUS_PACK' or defaultItemId = 'TREASURE_BOX' or defaultItemId = 'GIFT_BOX') and defaultItem = false) " + 
				"or defaultItem = true " + "order by type asc, defaultItemId asc")
				.list();

		HibernateManager.endSession();

		ArrayList<ClientItemJsonHelper> itemJsons = new ArrayList<ClientItemJsonHelper>();
		for (ItemBoxModel itemBoxModel : models) {

			if ((!useDefault(itemBoxModel)) && itemBoxModel.isDefaultItem()) {
				continue;
			}

			ClientItemJsonHelper json = new ClientItemJsonHelper();
			json.setItemId(useDefault(itemBoxModel) ? itemBoxModel.getDefaultItemId() : itemBoxModel.getItemId());
			json.setIconId(itemBoxModel.getIconId());
			json.setName(itemBoxModel.getName());
			json.setClassId(itemBoxModel.getClassId());
			json.setType(itemBoxModel.getType());
			json.setNeedTarget(itemBoxModel.isNeedTarget());
			json.setFunctionDesc(itemBoxModel.getFunctionDesc());
			json.setAnimationId(itemBoxModel.getAnimationId() == null ? "" : itemBoxModel.getAnimationId());
			json.setUseLevel(itemBoxModel.getUseLevel());
			json.setUseVipLevel(itemBoxModel.getUseVipLevel());
			json.setStackable(itemBoxModel.isStackable());
			
			json.setCanTrade(itemBoxModel.isCanTrade());

			// 機台卡
			json.setRefineCrystalCostList(itemBoxModel.getRefineCrystalCostList());
			json.setRefineLevelBetCount(itemBoxModel.getRefineLevelBetCount());
			json.setUseGame(itemBoxModel.getUseGame() == null ? "" : itemBoxModel.getUseGame().toString());
			json.setGameCardType(itemBoxModel.getGameCardType() == null ? "" : itemBoxModel.getGameCardType().toString());
			json.setNotUseGames(itemBoxModel.getNotUseGames());

			// 升級寶箱
			json.setTreasureBoxType(itemBoxModel.getTreasureBoxType());

			ArrayList<ItemJsonContent> fixItemList = new ArrayList<ItemJsonContent>();
			if (itemBoxModel.getFixItemListClient() != null) {
				Type type = new TypeToken<ArrayList<ItemJsonContent>>() {}.getType();
				fixItemList = new Gson().fromJson(itemBoxModel.getFixItemListClient(), type);
			}
			json.setFixItemListClient(fixItemList);

			ArrayList<ItemJsonContent> vipExtraGetItemList = new ArrayList<ItemJsonContent>();
			if (itemBoxModel.getVipExtraGetItemListClient() != null) {
				Type type = new TypeToken<ArrayList<ItemJsonContent>>() {
				}.getType();
				vipExtraGetItemList = new Gson().fromJson(itemBoxModel.getVipExtraGetItemListClient(), type);
			}
			json.setVipExtraGetItemListClient(vipExtraGetItemList);

			json.setExtraGetVipLevel(itemBoxModel.getExtraGetVipLevel());

			itemJsons.add(json);
		}

		String json = new GsonBuilder().setPrettyPrinting().create().toJson(itemJsons);
		log.error(json);

	}

	private String itemId;
	private String name;
	private String functionDesc;
	private String iconId;
	private Integer useLevel;
	private Integer useVipLevel;
	private Boolean needTarget;
	private Integer type;
	private Integer classId;
	private Boolean stackable;
	private Boolean canTrade;
	private String animationId;
	private ArrayList<Integer> refineCrystalCostList;
	private ArrayList<Integer> refineLevelBetCount;

	private Byte treasureBoxType;
	private Integer extraGetVipLevel;
	private ArrayList<ItemJsonContent> fixItemListClient;
	private ArrayList<ItemJsonContent> vipExtraGetItemListClient;

	private String useGame;
	private ArrayList<String> notUseGames;
	private String gameCardType;

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getIconId() {
		return iconId;
	}

	public void setIconId(String iconId) {
		this.iconId = iconId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Boolean getNeedTarget() {
		return needTarget;
	}

	public void setNeedTarget(Boolean needTarget) {
		this.needTarget = needTarget;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Integer getClassId() {
		return classId;
	}

	public void setClassId(Integer classId) {
		this.classId = classId;
	}

	public String getFunctionDesc() {
		return functionDesc;
	}

	public void setFunctionDesc(String functionDesc) {
		this.functionDesc = functionDesc;
	}

	public Boolean getStackable() {
		return stackable;
	}

	public void setStackable(Boolean stackable) {
		this.stackable = stackable;
	}

	public String getAnimationId() {
		return animationId;
	}

	public void setAnimationId(String animationId) {
		this.animationId = animationId;
	}

	public Integer getUseLevel() {
		return useLevel;
	}

	public void setUseLevel(Integer useLevel) {
		this.useLevel = useLevel;
	}

	public String getUseGame() {
		return useGame;
	}

	public void setUseGame(String useGame) {
		this.useGame = useGame;
	}

	public ArrayList<Integer> getRefineCrystalCostList() {
		return refineCrystalCostList;
	}

	public void setRefineCrystalCostList(ArrayList<Integer> refineCrystalCostList) {
		this.refineCrystalCostList = refineCrystalCostList;
	}

	public Integer getUseVipLevel() {
		return useVipLevel;
	}

	public void setUseVipLevel(Integer useVipLevel) {
		this.useVipLevel = useVipLevel;
	}

	public Boolean getCanTrade() {
		return canTrade;
	}

	public void setCanTrade(Boolean canTrade) {
		this.canTrade = canTrade;
	}

	public Byte getTreasureBoxType() {
		return treasureBoxType;
	}

	public void setTreasureBoxType(Byte treasureBoxType) {
		this.treasureBoxType = treasureBoxType;
	}

	public Integer getExtraGetVipLevel() {
		return extraGetVipLevel;
	}

	public void setExtraGetVipLevel(Integer extraGetVipLevel) {
		this.extraGetVipLevel = extraGetVipLevel;
	}

	public ArrayList<ItemJsonContent> getFixItemListClient() {
		return fixItemListClient;
	}

	public void setFixItemListClient(ArrayList<ItemJsonContent> fixItemListClient) {
		this.fixItemListClient = fixItemListClient;
	}

	public ArrayList<ItemJsonContent> getVipExtraGetItemListClient() {
		return vipExtraGetItemListClient;
	}

	public void setVipExtraGetItemListClient(ArrayList<ItemJsonContent> vipExtraGetItemListClient) {
		this.vipExtraGetItemListClient = vipExtraGetItemListClient;
	}

	public String getGameCardType() {
		return gameCardType;
	}

	public void setGameCardType(String gameCardType) {
		this.gameCardType = gameCardType;
	}


	public ArrayList<String> getNotUseGames() {
		return notUseGames;
	}


	public void setNotUseGames(ArrayList<String> notUseGames) {
		this.notUseGames = notUseGames;
	}


	public ArrayList<Integer> getRefineLevelBetCount() {
		return refineLevelBetCount;
	}


	public void setRefineLevelBetCount(ArrayList<Integer> refineLevelBetCount) {
		this.refineLevelBetCount = refineLevelBetCount;
	}
}
